package edu.pku.sei.mte.transformation.expression;

import java.util.List;

import edu.pku.sei.mte.mtemodel.model.MAttribute;
import edu.pku.sei.mte.mtemodel.mtmodel.rulebased.Rule;
import edu.pku.sei.mte.runtime.Context;
import edu.pku.sei.mte.runtime.rulebased.RBTContext;
import edu.pku.sei.mte.transformation.ModelTransformationVisitor;
import edu.pku.sei.mte.transformation.VisitorFactory;

public class CheckOnlyExpressionVisitor extends EvaluateExpressionVisitor {

	public CheckOnlyExpressionVisitor(RBTContext context) {
		super(context);
	}

	public CheckOnlyExpressionVisitor(RBTContext context, Object expectedValue) {
		super(context, expectedValue);
	}

	@Override
	protected void enforceBindExpression(Object expectedValue, MAttribute attr) {
		return;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Object enforceCallRelationExpression(Rule rule, RBTContext cc) throws Exception {
		ModelTransformationVisitor visitor = VisitorFactory.INSTANCE.getQVTCheckResultVisitor(cc.getEnvironment());
		visitor.getContextStack().push(cc);
		List<Context> ccs = (List<Context>) rule.accept(visitor);
		visitor.getContextStack().pop();
		
		if(ccs==null){
			return Boolean.TRUE;
		} else if(ccs.size()==0)
			return Boolean.FALSE;
		else if(ccs.size()>1){
			System.out.println("more satisfied context in relation call expression");
		}
		return ccs.get(0);
	}

	@Override
	protected boolean isCheckOnly() {
		return true;
	}

	@Override
	protected boolean isEnforce() {
		return false;
	}

	@Override
	protected EvaluateExpressionVisitor getSameTypeVisitor(RBTContext context) {
		return VisitorFactory.INSTANCE.getCheckOnlyExpressionVisitor(context);
	}

	@Override
	protected EvaluateExpressionVisitor getSameTypeVisitor(RBTContext context,
			Object expectedValue) {
		return VisitorFactory.INSTANCE.getCheckOnlyExpressionVisitor(context, expectedValue);
	}

}
